package com.liuhaozzu.zookeeper.zookeeper_app.constructor;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;

public class ZooKeeper_With_SID_PASSWD implements Watcher {

	private static CountDownLatch connectedSemaphore=new CountDownLatch(1);

	public static void main(String[] args) throws IOException, InterruptedException {
		ZooKeeper zooKeeper=new ZooKeeper("hadoop1:2181",5000, new ZooKeeper_With_SID_PASSWD());
		connectedSemaphore.await();
		long sessionId=zooKeeper.getSessionId();
		byte[] passwd=zooKeeper.getSessionPasswd();
		System.out.println("sessionId:"+sessionId+",pssswd:"+new String(passwd));
		zooKeeper=new ZooKeeper("hadoop1:2181", 5000, new ZooKeeper_With_SID_PASSWD(), 1l, "test".getBytes());
		System.out.println("1111111>>>>>>"+zooKeeper);
		zooKeeper= new ZooKeeper("hadoop1:2181",5000,new ZooKeeper_With_SID_PASSWD(),sessionId,passwd);
		System.out.println("2222222>>>>>>>"+zooKeeper);
		Thread.sleep(Integer.MAX_VALUE);
	}
	@Override
	public void process(WatchedEvent event) {
		System.out.println("Received watched event: "+event);
		if (KeeperState.SyncConnected==event.getState()) {
			connectedSemaphore.countDown();
		}
	}
	
}
